add_libc_testsuite(libc_math_unittests)

function(add_fp_unittest name)
  cmake_parse_arguments(
    "MATH_UNITTEST"
    "NEED_MPFR" # No optional arguments
    "" # Single value arguments
    "" # Multi-value arguments
    ${ARGN}
  )

  if(MATH_UNITTEST_NEED_MPFR)
    if(NOT LIBC_TESTS_CAN_USE_MPFR)
      message("WARNING: Math test ${name} will be skipped as MPFR library is not available.")
      return()
    endif()
  endif()

  add_libc_unittest(${name} ${MATH_UNITTEST_UNPARSED_ARGUMENTS})
  get_fq_target_name(${name} fq_target_name)
  if (NOT TARGET ${fq_target_name})
    return()
  endif()
  if(MATH_UNITTEST_NEED_MPFR)
    target_link_libraries(${fq_target_name} PRIVATE libcMPFRWrapper -lmpfr -lgmp)
  endif()
  target_link_libraries(${fq_target_name} PRIVATE LibcFPTestHelpers)
endfunction(add_fp_unittest)

add_fp_unittest(
  cosf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    cosf_test.cpp
  HDRS
    sdcomp26094.h
  DEPENDS
    libc.include.errno
    libc.src.math.cosf
    libc.utils.CPP.standalone_cpp
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  sinf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    sinf_test.cpp
  HDRS
    sdcomp26094.h
  DEPENDS
    libc.include.errno
    libc.src.math.sinf
    libc.utils.CPP.standalone_cpp
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  sincosf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    sincosf_test.cpp
  HDRS
    sdcomp26094.h
  DEPENDS
    libc.include.errno
    libc.src.math.sincosf
    libc.utils.CPP.standalone_cpp
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fabs_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    fabs_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fabs
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fabsf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    fabsf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fabsf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fabsl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    fabsl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fabsl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  trunc_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    trunc_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.trunc
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  truncf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    truncf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.truncf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  truncl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    truncl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.truncl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ceil_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    ceil_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.ceil
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ceilf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    ceilf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.ceilf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ceill_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    ceill_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.ceill
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  floor_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    floor_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.floor
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  floorf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    floorf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.floorf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  floorl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    floorl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.floorl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  round_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    round_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.round
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  roundf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    roundf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.roundf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  roundl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    roundl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.roundl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  lround_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    lround_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.errno
    libc.include.math
    libc.src.errno.__errno_location
    libc.src.fenv.feclearexcept
    libc.src.fenv.feraiseexcept
    libc.src.fenv.fetestexcept
    libc.src.math.lround
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  lroundf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    lroundf_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.errno
    libc.include.math
    libc.src.errno.__errno_location
    libc.src.fenv.feclearexcept
    libc.src.fenv.feraiseexcept
    libc.src.fenv.fetestexcept
    libc.src.math.lroundf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  lroundl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    lroundl_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.errno
    libc.include.math
    libc.src.errno.__errno_location
    libc.src.fenv.feclearexcept
    libc.src.fenv.feraiseexcept
    libc.src.fenv.fetestexcept
    libc.src.math.lroundl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  llround_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    llround_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.errno
    libc.include.math
    libc.src.errno.__errno_location
    libc.src.fenv.feclearexcept
    libc.src.fenv.feraiseexcept
    libc.src.fenv.fetestexcept
    libc.src.math.llround
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  llroundf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    llroundf_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.errno
    libc.include.math
    libc.src.errno.__errno_location
    libc.src.fenv.feclearexcept
    libc.src.fenv.feraiseexcept
    libc.src.fenv.fetestexcept
    libc.src.math.llroundf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  llroundl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    llroundl_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.errno
    libc.include.math
    libc.src.errno.__errno_location
    libc.src.fenv.feclearexcept
    libc.src.fenv.feraiseexcept
    libc.src.fenv.fetestexcept
    libc.src.math.llroundl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  rint_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    rint_test.cpp
  HDRS
    RIntTest.h
  DEPENDS
    libc.include.math
    libc.src.math.rint
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  rintf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    rintf_test.cpp
  HDRS
    RIntTest.h
  DEPENDS
    libc.include.math
    libc.src.math.rintf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  rintl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    rintl_test.cpp
  HDRS
    RIntTest.h
  DEPENDS
    libc.include.math
    libc.src.math.rintl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  lrint_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    lrint_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.math
    libc.src.math.lrint
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  lrintf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    lrintf_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.math
    libc.src.math.lrintf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  lrintl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    lrintl_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.math
    libc.src.math.lrintl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  llrint_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    llrint_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.math
    libc.src.math.llrint
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  llrintf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    llrintf_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.math
    libc.src.math.llrintf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  llrintl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    llrintl_test.cpp
  HDRS
    RoundToIntegerTest.h
  DEPENDS
    libc.include.math
    libc.src.math.llrintl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  expf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    expf_test.cpp
  DEPENDS
    libc.include.errno
    libc.include.math
    libc.src.math.expf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  exp2f_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    exp2f_test.cpp
  DEPENDS
    libc.include.errno
    libc.include.math
    libc.src.math.exp2f
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  copysign_test
  SUITE
    libc_math_unittests
  SRCS
    copysign_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.copysign
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  copysignf_test
  SUITE
    libc_math_unittests
  SRCS
    copysignf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.copysignf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  copysignl_test
  SUITE
    libc_math_unittests
  SRCS
    copysignl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.copysignl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  frexp_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    frexp_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.frexp
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  frexpf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    frexpf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.frexpf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  frexpl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    frexpl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.frexpl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ilogb_test
  SUITE
    libc_math_unittests
  SRCS
    ilogb_test.cpp
  HDRS
    ILogbTest.h
  DEPENDS
    libc.include.math
    libc.src.math.ilogb
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ilogbf_test
  SUITE
    libc_math_unittests
  SRCS
    ilogbf_test.cpp
  HDRS
    ILogbTest.h
  DEPENDS
    libc.include.math
    libc.src.math.ilogbf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ilogbl_test
  SUITE
    libc_math_unittests
  SRCS
    ilogbl_test.cpp
  HDRS
    ILogbTest.h
  DEPENDS
    libc.include.math
    libc.src.math.ilogbl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ldexp_test
  SUITE
    libc_math_unittests
  SRCS
    ldexp_test.cpp
  HDRS
    LdExpTest.h
  DEPENDS
    libc.include.math
    libc.src.math.ldexp
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ldexpf_test
  SUITE
    libc_math_unittests
  SRCS
    ldexpf_test.cpp
  HDRS
    LdExpTest.h
  DEPENDS
    libc.include.math
    libc.src.math.ldexpf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  ldexpl_test
  SUITE
    libc_math_unittests
  SRCS
    ldexpl_test.cpp
  HDRS
    LdExpTest.h
  DEPENDS
    libc.include.math
    libc.src.math.ldexpl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  logb_test
  SUITE
    libc_math_unittests
  SRCS
    logb_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.logb
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  logbf_test
  SUITE
    libc_math_unittests
  SRCS
    logbf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.logbf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  logbl_test
  SUITE
    libc_math_unittests
  SRCS
    logbl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.logbl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  modf_test
  SUITE
    libc_math_unittests
  SRCS
    modf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.modf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  modff_test
  SUITE
    libc_math_unittests
  SRCS
    modff_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.modff
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  modfl_test
  SUITE
    libc_math_unittests
  SRCS
    modfl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.modfl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fdimf_test
  SUITE
    libc_math_unittests
  SRCS
    fdimf_test.cpp
  HDRS
    FDimTest.h
  DEPENDS
    libc.include.math
    libc.src.math.fdimf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fdim_test
  SUITE
    libc_math_unittests
  SRCS
    fdim_test.cpp
  HDRS
    FDimTest.h
  DEPENDS
    libc.include.math
    libc.src.math.fdim
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fdiml_test
  SUITE
    libc_math_unittests
  SRCS
    fdiml_test.cpp
  HDRS
    FDimTest.h
  DEPENDS
    libc.include.math
    libc.src.math.fdiml
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fminf_test
  SUITE
    libc_math_unittests
  SRCS
    fminf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fminf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fmin_test
  SUITE
    libc_math_unittests
  SRCS
    fmin_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fmin
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fminl_test
  SUITE
    libc_math_unittests
  SRCS
    fminl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fminl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fmaxf_test
  SUITE
    libc_math_unittests
  SRCS
    fmaxf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fmaxf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fmax_test
  SUITE
    libc_math_unittests
  SRCS
    fmax_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fmax
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fmaxl_test
  SUITE
    libc_math_unittests
  SRCS
    fmaxl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fmaxl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  sqrtf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    sqrtf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.sqrtf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  sqrt_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    sqrt_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.sqrt
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  sqrtl_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    sqrtl_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.sqrtl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  remquof_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    remquof_test.cpp
  HDRS
    RemQuoTest.h
  DEPENDS
    libc.include.math
    libc.src.math.remquof
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  remquo_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    remquo_test.cpp
  HDRS
    RemQuoTest.h
  DEPENDS
    libc.include.math
    libc.src.math.remquo
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  remquol_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    remquol_test.cpp
  HDRS
    RemQuoTest.h
  DEPENDS
    libc.include.math
    libc.src.math.remquol
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  hypotf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    hypotf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.hypotf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  hypot_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    hypot_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.hypot
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  nextafter_test
  SUITE
    libc_math_unittests
  SRCS
    nextafter_test.cpp
  HDRS
    NextAfterTest.h
  DEPENDS
    libc.include.math
    libc.src.math.nextafter
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  nextafterf_test
  SUITE
    libc_math_unittests
  SRCS
    nextafterf_test.cpp
  HDRS
    NextAfterTest.h
  DEPENDS
    libc.include.math
    libc.src.math.nextafterf
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  nextafterl_test
  SUITE
    libc_math_unittests
  SRCS
    nextafterl_test.cpp
  HDRS
    NextAfterTest.h
  DEPENDS
    libc.include.math
    libc.src.math.nextafterl
    libc.utils.FPUtil.fputil
)

add_fp_unittest(
  fmaf_test
  NEED_MPFR
  SUITE
    libc_math_unittests
  SRCS
    fmaf_test.cpp
  DEPENDS
    libc.include.math
    libc.src.math.fmaf
    libc.utils.FPUtil.fputil
)
